﻿Public Class FormProduct

    Private Sub FormProduct_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        GUIHelper.FormatDataGridView(dgvProduct)

        cboType.ValueMember = "TypeId"
        cboType.DisplayMember = "TypeName"
        cboType.DataSource = Sql.ExecuteDataTable("SELECT TypeId,TypeName FROM Type WHERE Active=1 UNION SELECT 0,N'គ្រប់ប្រភេទ'")


        LoadData()
    End Sub

    Public Sub LoadData()

        Dim s = "SELECT p.*,c.CategoryName,t.TypeName,c.TypeId FROM Product p INNER JOIN (Category c INNER JOIN Type t ON t.TypeId=c.TypeId) ON c.CategoryId=p.CategoryId WHERE (p.ProductCode LIKE N'%" & txtSearch.Text & "%' OR p.ProductName LIKE N'%" & txtSearch.Text & "%' OR c.CategoryName LIKE N'%" & txtSearch.Text & "%' OR t.TypeName LIKE N'%" & txtSearch.Text & "%'  ) AND p.Active=1 "
        If cboType.SelectedValue <> 0 Then
            s = s & " AND t.TypeId=" & cboType.SelectedValue
        End If
        If cboCategory.SelectedValue <> 0 Then
            s = s & " AND c.CategoryId =" & cboCategory.SelectedValue
        End If
        Me.dgvProduct.DataSource = Sql.ExecuteDataTable(s)

    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If Me.dgvProduct.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        If MessageBox.Show("តើលោកអ្នកចង់លុបទំនិញមួយនេះមែនទេ?", "លុបទំនិញ", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
            Exit Sub
        End If

        Dim row As DataGridViewRow = Me.dgvProduct.SelectedRows(0)
        Dim id = row.Cells("ProductId").Value

        Sql.ExecuteNonQuery("UPDATE Product SET Active=0 WHERE ProductId=" & id)

        LoadData()
    End Sub

    Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        LoadData()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim frm As New FormProductEditor()
        frm.cboTypeName.SelectedValue = cboType.SelectedValue
        frm.cboCategoryName.SelectedValue = cboCategory.SelectedValue
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            LoadData()
        End If
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        If Me.dgvProduct.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        Dim row As DataGridViewRow = Me.dgvProduct.SelectedRows(0)
        Dim ProductId = row.Cells("ProductId").Value
        Dim ProductCode = row.Cells("ProductCode").Value
        Dim ProductName = row.Cells("ProductName").Value
        Dim CategoryId = row.Cells("CategoryId").Value
        Dim Price = row.Cells("Price").Value
        Dim MinimumPrice = row.Cells("MinimumPrice").Value
        Dim TypeId = row.Cells("TypeId").Value

        Dim frm As New FormProductEditor()
        frm.cboTypeName.SelectedValue = TypeId
        frm.txtProductId.Enabled = False
        frm.txtProductId.Text = ProductId
        frm.txtProductCode.Text = ProductCode
        frm.txtProductName.Text = ProductName
        frm.cboCategoryName.SelectedValue = CategoryId
        frm.txtPrice.Text = Price
        frm.txtMinimumPrice.Text = MinimumPrice

        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            LoadData()
        End If
    End Sub

    Private Sub cboType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboType.SelectedIndexChanged

        cboCategory.ValueMember = "CategoryId"
        cboCategory.DisplayMember = "CategoryName"
        cboCategory.DataSource = Sql.ExecuteDataTable("SELECT CategoryId,CategoryName FROM Category WHERE Active=1 AND TypeId=" & cboType.SelectedValue & " UNION SELECT 0,N'គ្រប់ក្រុមទំនិញ'")

        LoadData()
    End Sub

    Private Sub cboCategory_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCategory.SelectedIndexChanged

        LoadData()

    End Sub


End Class